﻿using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Windows.Controls;

using Gamey.Genesis;

using BindingSource = System.Windows.Forms.BindingSource;

namespace Gamey.UI
{
    /// <summary>
    /// Interaction logic for GenesisControl.xaml
    /// </summary>
    public partial class GenesisControl : UserControl
    {
        GameyDataSet dataSet;
        BindingSource bindingSource;
        BackgroundWorker gameLoader;

        public event EventHandler LoadedGames;

        public GenesisControl()
        {
            this.dataSet = new GameyDataSet();
            this.bindingSource = new BindingSource();
            this.gameLoader = new BackgroundWorker();
            this.gameLoader.DoWork += gameLoader_DoWork;
            this.gameLoader.RunWorkerCompleted += gameLoader_RunWorkerCompleted;

            InitializeComponent();
        }

        public void LoadGames()
        {
            if (!string.IsNullOrEmpty(Env.Settings.GenesisRomsPath))
            {
                this.statusLabel.Content = "Loading games...";
                this.gameLoader.RunWorkerAsync();
            }
            else
            {
                OnLoadedGames();
            }
        }

        public void LaunchSelectedGame()
        {
            var selectedItem = this.dataGrid.SelectedItem as DataRowView;
            if (selectedItem == null)
            {
                return;
            }

            var selectedRow = selectedItem.Row as GameyDataSet.GenesisGamesRow;
            if (selectedRow == null)
            {
                return;
            }

            string gensPath = Env.Settings.GensPath;
            string file = Path.Combine(Env.Settings.GenesisRomsPath, selectedRow.File);
            string gensDirectoryPath = Path.GetDirectoryName(gensPath);

            var gensProcessInfo = new ProcessStartInfo
            {
                FileName = gensPath,
                Arguments = string.Format("\"{0}\"", file),
                WorkingDirectory = gensDirectoryPath
            };

            Process.Start(gensProcessInfo);
        }

        protected void OnLoadedGames()
        {
            this.statusBar.Items.Remove(this.progressBarItem);
            this.statusLabel.Content = "Ready";

            EventHandler loadedGamesHandler = LoadedGames;
            if (loadedGamesHandler != null)
            {
                loadedGamesHandler(this, EventArgs.Empty);
            }
        }

        void gameLoader_DoWork(object sender, DoWorkEventArgs e)
        {
            new GenesisGameLoader().LoadGames(this.dataSet.GenesisGames);
        }

        void gameLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.bindingSource.DataSource = this.dataSet;
            this.bindingSource.DataMember = this.dataSet.GenesisGames.TableName;
            this.dataGrid.ItemsSource = this.bindingSource;

            OnLoadedGames();
        }

        private void dataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            LaunchSelectedGame();
        }

        private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.bindingSource.Filter = string.Format("Name LIKE '{0}%' OR File LIKE '%{0}%'", this.searchTextBox.Text);
        }
    }
}
